# Maintainer: András Svraka <svraka.andras@gmail.com>

_realname=emacs-pdf-tools-server
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.0.0
pkgrel=1
pkgdesc="Emacs support library for PDF files"
source=("${_realname}-${pkgver}.tar.gz::https://github.com/vedang/pdf-tools/archive/refs/tags/v${pkgver}.tar.gz")
noextract=(${_realname}-${pkgver}.tar.gz)
sha256sums=("b2b3bca06f838c2bc7219e1c1b50acae2b19ebd5af30980cb34211e6a69399cf")
mingw_arch=('mingw64' 'mingw32' 'ucrt64' 'clang64' 'clang32')
arch=("any")
url="https://github.com/vedang/pdf-tools"
license=("spdx:GPL-3.0")
depends=("${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-poppler")
optdepends=("${MINGW_PACKAGE_PREFIX}-imagemagick")
makedepends=("${MINGW_PACKAGE_PREFIX}-emacs"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")

prepare() {
  # Workaround for symlinks in tarball and the different package and git repo
  # names.
  mkdir -p "${srcdir}/${_realname}-${pkgver}"
  tar xzf "${srcdir}/${_realname}-${pkgver}.tar.gz" \
    -C "${srcdir}/${_realname}-${pkgver}" \
    --strip-components=1 \
    --exclude "README"

  cd "${srcdir}/${_realname}-${pkgver}/server"

  autoreconf -fvi
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  CXXFLAGS=-std=c++17 ../"${_realname}-${pkgver}"/server/configure \
    --prefix="${MINGW_PREFIX}" \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --target="${MINGW_CHOST}"

  make
}

package() {
  pushd "${srcdir}/build-${MSYSTEM}"
  make install DESTDIR="${pkgdir}"
  popd

  pushd "${srcdir}/${_realname}-${pkgver}"
  install --target-directory=${pkgdir}${MINGW_PREFIX}/share/doc/${_realname} \
          -D --mode=644 \
          README.org NEWS
  install --target-directory=${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname} \
          -D --mode=644 \
          COPYING COPYING.SYNCTEX
  popd
}
